//
//  NewPassWordView.m
//  TaiHao
//
//  Created by rzm on 16/5/30.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxSetNewPassWordView.h"
#import "IsValidateOrNot.h"
#import "SampuxCustomTextFiledView.h"

@interface SampuxSetNewPassWordView ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *installNewPWF;

@property (nonatomic, strong) UITextField *makeSurePWF;
@property (nonatomic, strong) UIView *backView ;

@property (nonatomic, strong) SampuxCustomTextFiledView *installNewView;
@property (nonatomic, strong) SampuxCustomTextFiledView *makeSurePWView;
@property(nonatomic,assign) BOOL isConnectOrNot;

@end

@implementation SampuxSetNewPassWordView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.isConnectOrNot = YES;

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setNewPassword:) name:NOTIFICATION_RESPONSE_FORGETPWD_NEWPWD object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(connectFail) name:@"socketConnectFail" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectSuccess) name:@"socketConnectSuccess" object:nil];
        [self loadUI];
    }
    return self;
}
- (void)connectSuccess
{
    self.isConnectOrNot = YES;
}

- (void)connectFail
{
    _isConnectOrNot = NO;
}

- (void)loadUI
{    
    CGFloat font = 8;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 17.;
    }
    
    
    UIView *newPWView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, self.height - 64)];
    [self addSubview:newPWView];

    UIButton *iconImageView = [UIButton createButtonWithImage:imageNamed(@"sampux_logo")];
    [newPWView addSubview:iconImageView];
    iconImageView.frame = CGRectMake(0, newPWView.height * 0.08, newPWView.width, newPWView.height *0.15);
    iconImageView.userInteractionEnabled = NO;
    
    CGRect installNewPWRect = CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2 , iconImageView.bottom + Sampux6HScale(30),  Sampux6WScale(SampuxCustomW), Sampux6HScale(SampuxCustomH));
    _installNewView = [[SampuxCustomTextFiledView alloc]initWithFrame:installNewPWRect];
    [newPWView addSubview:_installNewView];
    [_installNewView createUserTextFiledWithPlaceholder:@"输入新密码" andLeftImage:@"lock" andRightImage:@"eye"];
    _installNewView.textFiled.secureTextEntry = YES;
    _installNewView.textFiled.delegate = self;
    
    
//    UITextField *installNewPWF = [UITextField createTextFieldWithRect:installNewPWRect andPlaceholder:@"请输入新密码" andLeftImage:@"lock"];//
//    installNewPWF.delegate = self;
//    installNewPWF.tag = 500;
//    self.installNewPWF = installNewPWF;
//    [newPWView addSubview:installNewPWF];

    
    CGRect makeSurePWFRect = CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2, CGRectGetMaxY(_installNewView.frame) + SampuxMargin, Sampux6WScale(SampuxCustomW),  Sampux6HScale(SampuxCustomH));
    _makeSurePWView = [[SampuxCustomTextFiledView alloc]initWithFrame:makeSurePWFRect];
    [newPWView addSubview:_makeSurePWView];
    [_makeSurePWView createUserTextFiledWithPlaceholder:@"确认新密码" andLeftImage:@"lock" andRightImage:@"eye"];
    _makeSurePWView.textFiled.secureTextEntry = YES;
    _makeSurePWView.textFiled.delegate = self;

//    UITextField *makeSurePWF = [UITextField createTextFieldWithRect:makeSurePWFRect andPlaceholder:@"确认新密码" andLeftImage:@"lock"];
//    makeSurePWF.delegate = self;
//    self.makeSurePWF = makeSurePWF;
//    [newPWView addSubview:makeSurePWF];
    UIButton *confirmBtn = [UIButton createButtonWithTitle:@"确定" frame:CGRectMake(self.width/2 -  Sampux6WScale(354/2)/2, CGRectGetMaxY(_makeSurePWView.frame) + Sampux6HScale(30), Sampux6WScale(354/2),  Sampux6HScale(SampuxCustomH)) titleFont:THFont(font) titleColor:THDeFaultBlueColor];
    confirmBtn.layer.borderWidth = 1;
    confirmBtn.layer.borderColor = THWhiteAlplaColor(0.6).CGColor;
    [newPWView addSubview:confirmBtn];
    [confirmBtn setCorner:confirmBtn.height/2];
    [confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)confirmBtnClick
{
    BOOL isPassword = [IsValidateOrNot isValidatePassword:self.installNewView.textFiled.text];
    if ([self.installNewView.textFiled.text isEqualToString: @""]|| self.installNewView.textFiled.text.length == 0)
    {
        [SVProgressHUD showErrorWithStatus:@"密码必须为7~16位长度的字母，数字，下划线"];

//        [MBProgressHUD showError:@"密码必须为7~16位长度的字母，数字，下划线"];
    }else
    {
        if (!isPassword) {
            [SVProgressHUD showErrorWithStatus:@"密码必须为7~16位长度的字母，数字，下划线"];

//            [MBProgressHUD showError:@"密码必须为7~16位长度的字母，数字，下划线"];
        }else
        {
            if ([self.installNewView.textFiled.text isEqualToString: self.makeSurePWView.textFiled.text]) {
                if (self.isConnectOrNot == NO) {
                    [SVProgressHUD showErrorWithStatus:@"网络未连接，请检查您的网络连接设置"];
                }else
                {
                    [GBInterMaster_CommForSystem requestUserForgetPwdNewPwd:0 withPassword:self.installNewView.textFiled.text];
                }
            }else
            {
                [SVProgressHUD showErrorWithStatus:@"两次密码不一致"];
//                [MBProgressHUD showError:@"两次密码不一致"];
            }
        }
    }
}


- (void)setNewPassword:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"setNewPassword == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    if(responseCode == 0x00){
        [SVProgressHUD showSuccessWithStatus:@"修改密码成功"];
        if (self.btnDelegate && [self.btnDelegate respondsToSelector:@selector(clickConfirmBtn)]) {
            [self.btnDelegate clickConfirmBtn];
        }
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"修改密码失败"];
//        [MBProgressHUD showError:@"修改密码失败"];
    }
}

#pragma -mark UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if (textField == self.installNewView.textFiled ||textField == self.makeSurePWView.textFiled){
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (toBeString.length > 16 && range.length < 7){
            textField.text = [toBeString substringToIndex:16];
            return NO;
        }
        return YES;
    } else
    {
        return YES;
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self layoutIfNeeded];
    CGFloat keyboardHeight = 216.0f;
    self.frame = CGRectMake(0, -keyboardHeight/7 , self.width, kScreenHeight);
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
